package com.wangy.database.jdbc;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.apache.log4j.Logger;

public class JdbcTemplateProxy implements InvocationHandler{
	
	private Object target;
	private long timeCost;
	Logger logger = Logger.getLogger(this.getClass());
	
	public Object bind(Object target){
		this.target = target;
		return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
	}

	@Override
	public Object invoke(Object object, Method method, Object[] arg2)
			throws Throwable {
		Object result=null;
		beforeMethod(method);
		result = method.invoke(target, arg2);
		afterMethod(method);
		return result;
	}

	public Object getTarget() {
		return target;
	}

	public void setTarget(Object target) {
		this.target = target;
	}
	
	public void beforeMethod(Method method){
		timeCost = System.currentTimeMillis();
		logger.info(method.getName()+" operation is start...");
	}
	
	public void afterMethod(Method method){
		logger.info(method.getName()+" operation is end...");
		logger.info(method.getName()+" is cost "+(System.currentTimeMillis()-timeCost));
	}
	

}
